package com.homwee.factory.tvtool;

import com.homwee.factory.R;

import android.app.Service;
import android.content.Context;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TabHost;

import com.homwee.factory.tvtool.AlertDialog;
import com.homwee.factory.tvtool.StaticValues;

public class AQLayout extends TabHost {
	
	private Context mContext;
	private Service mService;
	private WindowManager mWindowManager;
	private long mExitTime;
	
	public AlertDialog alertDialog;
	public Button saveButton;
	
	public AQLayout(Context context, Service sv) {
		super(context);
		mContext = context;
		mService = sv;
		mWindowManager = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
		LayoutInflater.from(mContext).inflate(R.layout.aq, this);
		saveButton = (Button)findViewById(R.id.ExportAQData);
		View.OnClickListener saveListener = new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				alertDialog.hide();
				saveButton.performClick();
				
/*				mWindowManager.removeView(AQLayout.this);					
				mService.stopSelf();*/
			}
		};
		
		View.OnClickListener unSaveListener = new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				alertDialog.hide();
				mWindowManager.removeView(AQLayout.this);					
				mService.stopSelf();
				
			}
		};
		
		alertDialog = new AlertDialog(mContext,saveListener,unSaveListener);
		
		StaticValues.aqLayoutInstance = this;
		
	}
	
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		// TODO Auto-generated method stub
		switch(event.getKeyCode())
		{
		case KeyEvent.KEYCODE_BACK:
//			Toast.makeText(mContext, "rep back", Toast.LENGTH_SHORT).show();			
			if((System.currentTimeMillis() - mExitTime) > 1000)
			{
//				Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
				mExitTime = System.currentTimeMillis();
			}
			else
			{	
				StaticValues.bSaveAQAndExit = true;
				alertDialog.show();				
			}			
			return true;
			default:
				return super.dispatchKeyEvent(event);
		}

	}

	
	
}
